package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.palm.hubo2o.R;
import com.palm.hubo2o.adapter.HisIssueAdapter;
import com.palm.hubo2o.event.IssueHistoryEvent;
import com.palm.hubo2o.model.Issue;
import com.palm.hubo2o.util.EnumCode;
import com.palm.hubo2o.util.IssueCenter;
import com.palm.palmutils.widget.EasyRecyclerView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Created by wongqi on 16/6/15.
 */
public class PastPrizesDialogFragment extends BottomSheetDialogFragment {
    public static final String TAG = PastPrizesDialogFragment.class.getName();
    @Bind(R.id.recycler_view)
    EasyRecyclerView mRecyclerView;
    private EnumCode.gameId gameId;
    private HisIssueAdapter mAdapter;
    private List<Issue> mList;

    public static PastPrizesDialogFragment getInstance(EnumCode.gameId gameId) {
        PastPrizesDialogFragment dialog = new PastPrizesDialogFragment();
        dialog.setGameId(gameId);
        dialog.setPrizes(IssueCenter.open().switchIssueList(gameId));
        return dialog;
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();

    }

    /**
     * 接受历史期次变更消息
     */
    public void onEventMainThread(IssueHistoryEvent event) {
        if (event.lotteryId.equals(gameId)) {
            setPrizes(IssueCenter.open().switchIssueList(gameId));
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_past_prizes, container, false);
        ButterKnife.bind(this, v);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new HisIssueAdapter(gameId);
        mAdapter.setIssues(mList);
        mRecyclerView.setAdapter(mAdapter);
        return v;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void setGameId(EnumCode.gameId gameId) {
        this.gameId = gameId;
    }

    private void setPrizes(List<Issue> list) {
        mList = list;
        if (mAdapter != null) {
            mAdapter.setIssues(mList);
            mAdapter.notifyDataSetChanged();
        }
    }
}
